import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from project_utils.get_fridge_mappings import match_mappings, not_match_mappings
from config import *
from concat_sql.fridge_column_mapping import insert_fridge_mapping
import datetime
import multiprocessing

def get_result(future):
    print(future.result())


if __name__ == '__main__':
    # 历史所有日期
    begin = datetime.date(FRIDGE_START_YEAR, FRIDGE_START_MONTH, FRIDGE_START_DAY)
    end = datetime.date(FRIDGE_END_YEAR, FRIDGE_END_MONTH, FRIDGE_END_DAY)
    date_list = [(begin + datetime.timedelta(days=i)).strftime("%Y-%m-%d") for i in range((end - begin).days + 1)]
    results = []
    pool = multiprocessing.Pool(processes=80)
    for date in date_list:
        # 映射表名
        if date < '2022-01-13':
            tablename = mongo_map_table
        else:
            tablename = hbase_map_table
        mapping1 = match_mappings(tablename)
        mapping2 = not_match_mappings(tablename)
        mappings = {**mapping1, **mapping2}
        for model, column_mapping in mappings.items():
            insert_fridge_mapping(model, column_mapping, fridge_history_table, date)
            result = pool.apply_async(insert_fridge_mapping, (model, column_mapping, fridge_history_table, date))
            results.append(result)
    pool.close()
    pool.join()
